<?php
namespace app\index\controller;

use app\index\model\CertCompanyModel;
use app\index\model\CertEmployModel;
use app\index\model\CertModel;
use app\index\model\PersonModel;
use app\index\model\DeptModel;
use app\index\model\EnginJianModel;
use app\index\model\FlowLogModel;
use app\index\model\LabourGongModel;
use app\index\model\UserGangModel;
use app\index\model\UserModel;
use app\index\model\UserRoleModel;
use app\index\relevan\CertRelevan;
use app\index\service\CertService;
use app\index\service\ImportService;
use app\index\service\LabourService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Request;

ini_set('max_execution_time',0);
class Import extends Common
{
    public function import_labour_gong(Request $request)
    {
        if($request->isPost()){
            $param = $request->post();
            $optType = $request->param('type','add','string');
            $data = $param['data'];
            if(!is_array($data) || count($data) < 1){
                return outputJson(4000, '没有可导入的数据');
            }

            $error = '';
            foreach ($data as $k=>&$v){
                $hang = "行".($k + 1);

                if(!isset($v['name']) || empty($v['name'])){
                    $error = $hang."未输入姓名";break;
                }

                if(!isset($v['idcard']) || empty($v['idcard'])){
                    $error = $hang."未输入身份证号";break;
                }else{
                    if(!(new CertService())->check_person_idcard_mate($v['idcard'])){
                        $error = $hang."身份证号错误";break;
                    }
                }
                if((new LabourService())->check_labour_gong_name($v['name'], $v['idcard'])){
                    $error = $hang."人员已存在";break;
                }
                $data[$k]['sex'] = check_sex_by_idcard($v['idcard']);

                if(!isset($v['tel']) || empty($v['tel'])){
                    $error = $hang."未输入联系电话";break;
                }

                if(!isset($v['yongxing']) || empty($v['yongxing'])){
                    $error = $hang."未输入用工性质";break;
                }else{
                    $yongxing = (new LabourService())->get_labour_gong_yongxing();
                    if(!in_array($v['yongxing'], $yongxing)){
                        $error = $hang."用工性质不存在";break;
                    }
                }

                if(isset($v['gongtype']) && !empty($v['gongtype'])){
                    $is_base = (new ImportService())->check_basedata('engin_labour_gongtype',$v['gongtype']);
                    if($is_base !== true){
                        $error = $hang."工种错误，请输入以下".$is_base."其中一种";break;
                    }
                    $data[$k]['gongtype'] = (new ImportService())->get_basedata_keyid('engin_labour_gongtype',$v['gongtype']);
                    if(!$data[$k]['gongtype']){
                        $error = $hang."工种错误，请输入以下".$is_base."其中一种";break;
                    }
                }



                if(isset($v['areas']) && !empty($v['areas'])){
                    if(!(new ImportService())->check_areas($v['areas'], 3)){
                        $error = $hang."所在地区错误";break;
                    }
                }
            }

            if($error != ''){
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>$error]);
            }
            unset($v);
            Db::startTrans();
            try{
                foreach ($data as $k=>$v){
                    $v['comid'] = $this->userinfo['comid'];
                    $v['created_user'] = $this->userinfo['id'];
                    $v['created_at'] = date('Y-m-d H:i:s');
                    $personid = LabourGongModel::insertGetId($v);
                    (new OptService())->add_flow_log(FlowLogModel::SET_IMPORT,'labour_gong',$personid);
                    (new OptService())->add_log_list(FlowLogModel::SET_IMPORT, 'labour_gong', $personid, '工人花名册信息');
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                halt($e->getMessage());
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>"操作失败"]);
            }
            return outputJson(2000, lang('code_2000'), ['status'=>1]);
        }else{
            $sheet = $request->param('sheet', '', 'int');
            $start = $request->param('start', 1, 'int');
            if($sheet === ''){
                return outputJson(4000, "请选择Sheet");
            }

            $data['data'] = (new Upload())->get_excel_sheet_data($sheet, $start);
            if(!$data['data'] || !is_array($data['data']) || count($data['data']) < 1){
                return outputJson(4000, "未获取到有效数据");
            }

            $data['field'] = [
                ['field'=>'name', 'name'=>'姓名', 'ismust'=>1],
                ['field'=>'idcard', 'name'=>'身份证号', 'ismust'=>1],
                ['field'=>'tel', 'name'=>'联系电话', 'ismust'=>1],
                ['field'=>'yongxing', 'name'=>'用工性质', 'ismust'=>1],
                ['field'=>'gongtype', 'name'=>'工种', 'ismust'=>1],
                ['field'=>'areas', 'name'=>'所在地区', 'ismust'=>0],
                ['field'=>'addr', 'name'=>'详细地址', 'ismust'=>0],
                ['field'=>'bankname', 'name'=>'开户行', 'ismust'=>0],
                ['field'=>'banknum', 'name'=>'银行卡号', 'ismust'=>0],
                ['field'=>'explain', 'name'=>'备注', 'ismust'=>0],
            ];
            return outputJson(2000, lang('code_2000'), $data);
        }
    }

    public function import_engin_jian(Request $request)
    {
        if($request->isPost()){
            $param = $request->post();
            $optType = $request->param('type','add','string');
            $data = $param['data'];
            if(!is_array($data) || count($data) < 1){
                return outputJson(4000, '没有可导入的数据');
            }

            $error = '';
            foreach ($data as $k=>&$v){
                $hang = "行".($k + 1);


                if(!isset($v['jiantype']) || empty($v['jiantype'])){
                    $error = $hang."未输入检查项类别";break;
                }else{
                    $is_base = (new ImportService())->check_basedata('scene_jiantype',$v['jiantype']);
                    if($is_base !== true){
                        $error = $hang."检查项类别错误，请输入以下".$is_base."其中一种";break;
                    }
                    $data[$k]['jiantype'] = (new ImportService())->get_basedata_keyid('scene_jiantype',$v['jiantype']);
                    if(!$data[$k]['jiantype']){
                        $error = $hang."检查项类别错误，请输入以下".$is_base."其中一种";break;
                    }
                }

                if(!isset($v['name']) || empty($v['name'])){
                    $error = $hang."未输入检查项";break;
                }

            }

            if($error != ''){
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>$error]);
            }

            Db::startTrans();
            try{

                foreach ($data as $kk=>$vv){
                    $vv['comid'] = $this->userinfo['comid'];
                    $vv['created_user'] = $this->userinfo['id'];
                    $vv['created_at'] = date('Y-m-d H:i:s');
                    $jianid = EnginJianModel::insertGetId($vv);
                    (new OptService())->add_flow_log(FlowLogModel::SET_IMPORT,'engin_jian',$jianid);
                    (new OptService())->add_log_list(FlowLogModel::SET_IMPORT, 'engin_jian', $jianid, '检查项维护');
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>"操作失败"]);
            }
            return outputJson(2000, lang('code_2000'), ['status'=>1]);
        }else{
            $sheet = $request->param('sheet', '', 'int');
            $start = $request->param('start', 1, 'int');
            if($sheet === ''){
                return outputJson(4000, "请选择Sheet");
            }

            $data['data'] = (new Upload())->get_excel_sheet_data($sheet, $start);
            if(!$data['data'] || !is_array($data['data']) || count($data['data']) < 1){
                return outputJson(4000, "未获取到有效数据");
            }

            $data['field'] = [
                ['field'=>'jiantype', 'name'=>'检查项类别', 'ismust'=>1],
                ['field'=>'name', 'name'=>'检查项', 'ismust'=>1],
                ['field'=>'gaicon', 'name'=>'整改措施', 'ismust'=>0],
            ];
            return outputJson(2000, lang('code_2000'), $data);
        }
    }

    public function import_person(Request $request)
    {
        if($request->isPost()){
            $param = $request->post();
            $optType = $request->param('type','add','string');
            $data = $param['data'];
            if(!is_array($data) || count($data) < 1){
                return outputJson(4000, '没有可导入的数据');
            }

            $error = '';
            foreach ($data as $k=>&$v){
                $hang = "行".($k + 1);

                if(!isset($v['name']) || empty($v['name'])){
                    $error = $hang."未输入人员姓名";break;
                }else{
                    if((new CertService())->check_person_name($v['name'])){
                        $error = $hang."人员姓名已存在";break;
                    }
                }

                if(!isset($v['idcard']) || empty($v['idcard'])){
                    $error = $hang."未输入人员身份证号码";break;
                }else{
                    if(!(new CertService())->check_person_idcard_mate($v['idcard'])){
                        $error = $hang."人员身份证号码错误";break;
                    }
                    if((new CertService())->check_person_idcard_exist($v['idcard'])){
                        $error = $hang."人员身份证号码已存在";break;
                    }

                    $data[$k]['sex'] = check_sex_by_idcard($v['idcard']);
                }


                if(!isset($v['xueli']) || empty($v['xueli'])){
                    $error = $hang."未输入学历";break;
                }else{
                    if(!in_array($v['xueli'], (new CertService())->get_person_xueli())){
                        $error = $hang."学历不存在";break;
                    }
                }

                if(!isset($v['deptid']) || empty($v['deptid'])){
                    $error = $hang."未输入所在部门";break;
                }else{
                    $result = DeptModel::where(['deleted_user'=>0,'name'=>$v['deptid']])->find();
                    if(!$result){
                        $error = $hang."所在部门不存在";break;
                    }
                    $data[$k]['deptid'] = $result['id'];
                    $data[$k]['chanid'] = (new UserService())->get_user_chanid($result['id']);
                }

                //岗位
                if(isset($v['gangid']) && !empty($v['gangid'])){
                    $gangArr = explode(',', $v['gangid']);
                    $gangidArr = [];
                    foreach ($gangArr as $gangname){
                        $gid = UserGangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$gangname])->value('id');
                        if(!$gid){
                            $error = $hang."岗位名称“".$gangname."”不存在";break;
                        }
                        $gangidArr[] = $gid;
                    }
                    $data[$k]['gangid'] = implode(',', $gangidArr);
                }

                if(!isset($v['nature']) || empty($v['nature'])){
                    $error = $hang."未输入人员性质";break;
                }else{
                    if(!in_array($v['nature'], (new CertService())->get_person_nature())){
                        $error = $hang."人员性质不存在";break;
                    }
                }

                if(isset($v['intodt']) && !empty($v['intodt'])){
                    if(!check_date($v['intodt'], 1)){
                        $error = $hang."入职日期格式错误";break;
                    }
                }

                if(!isset($v['state']) || empty($v['state'])){
                    $error = $hang."未输入人员状态";break;
                }else{
                    if(!in_array($v['state'], (new CertService())->get_person_state())){
                        $error = $hang."人员状态不存在";break;
                    }
                }

                if(isset($v['laokaidt']) && !empty($v['laokaidt'])){
                    if(!check_date($v['laokaidt'], 1)){
                        $error = $hang."合同开始日期格式错误";break;
                    }
                }
                if(isset($v['laojiedt']) && !empty($v['laojiedt'])){
                    if(!check_date($v['laojiedt'], 1)){
                        $error = $hang."合同结束日期格式错误";break;
                    }
                }

                if(!isset($v['social']) || empty($v['social'])){
                    $error = $hang."未输入社保状态";break;
                }else{
                    if(!in_array($v['social'], (new CertService())->get_person_social())){
                        $error = $hang."社保状态不存在";break;
                    }
                }

                if(isset($v['socarea']) && !empty($v['socarea'])){
                    if(!(new ImportService())->check_areas($v['socarea'], 2)){
                        $error = $hang."参保地错误";break;
                    }
                }

                if(isset($v['socstartdt']) && !empty($v['socstartdt'])){
                    if(!check_date($v['socstartdt'], 3)){
                        $error = $hang."参保开始月份格式错误";break;
                    }
                }
                if(isset($v['socenddt']) && !empty($v['socenddt'])){
                    if(!check_date($v['socenddt'], 3)){
                        $error = $hang."参保结束月份格式错误";break;
                    }
                }

            }

            if($error != ''){
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>$error]);
            }
            unset($v);
            Db::startTrans();
            try{
                foreach ($data as $k=>$v){
                    $v['comid'] = $this->userinfo['comid'];
                    $v['created_user'] = $this->userinfo['id'];
                    $v['created_at'] = date('Y-m-d H:i:s');
                    $personid = PersonModel::insertGetId($v);
                    (new CertRelevan())->vate_person_info($personid);
                    (new OptService())->add_flow_log(FlowLogModel::SET_IMPORT,'cert_person',$personid);
                    (new OptService())->add_log_list(FlowLogModel::SET_IMPORT, 'cert_person', $personid, '人员信息');
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                halt($e->getMessage());
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>"操作失败"]);
            }
            return outputJson(2000, lang('code_2000'), ['status'=>1]);
        }else{
            $sheet = $request->param('sheet', '', 'int');
            $start = $request->param('start', 1, 'int');
            if($sheet === ''){
                return outputJson(4000, "请选择Sheet");
            }

            $data['data'] = (new Upload())->get_excel_sheet_data($sheet, $start);
            if(!$data['data'] || !is_array($data['data']) || count($data['data']) < 1){
                return outputJson(4000, "未获取到有效数据");
            }

            $data['field'] = [
                ['field'=>'name', 'name'=>'人员姓名', 'ismust'=>1],
                ['field'=>'number', 'name'=>'员工编号', 'ismust'=>0],
                ['field'=>'idcard', 'name'=>'身份证号', 'ismust'=>1],
                ['field'=>'tel', 'name'=>'联系电话', 'ismust'=>0],
                ['field'=>'xueli', 'name'=>'学历', 'ismust'=>1],
                ['field'=>'major', 'name'=>'专业', 'ismust'=>0],
                ['field'=>'deptid', 'name'=>'所在部门', 'ismust'=>1],
                ['field'=>'gangid', 'name'=>'岗位', 'ismust'=>0],
                ['field'=>'nature', 'name'=>'人员性质', 'ismust'=>1],
                ['field'=>'intodt', 'name'=>'入职日期', 'ismust'=>0],
                ['field'=>'state', 'name'=>'人员状态', 'ismust'=>1],
                ['field'=>'explain', 'name'=>'备注', 'ismust'=>0],
                ['field'=>'laokaidt', 'name'=>'合同开始日期', 'ismust'=>0],
                ['field'=>'laojiedt', 'name'=>'合同结束日期', 'ismust'=>0],
                ['field'=>'laoshuo', 'name'=>'劳动合同说明', 'ismust'=>0],
                ['field'=>'social', 'name'=>'社保状态', 'ismust'=>1],
                ['field'=>'socarea', 'name'=>'参保地', 'ismust'=>0],
                ['field'=>'socstartdt', 'name'=>'参保开始月份', 'ismust'=>0],
                ['field'=>'socenddt', 'name'=>'参保结束月份', 'ismust'=>0],
                ['field'=>'shenum', 'name'=>'社保卡号', 'ismust'=>0],
                ['field'=>'gongnum', 'name'=>'公积金号', 'ismust'=>0],
                ['field'=>'socshuo', 'name'=>'社保说明', 'ismust'=>0],
                ['field'=>'bankname', 'name'=>'账户名', 'ismust'=>0],
                ['field'=>'banknum', 'name'=>'账号', 'ismust'=>0],
                ['field'=>'bankkai', 'name'=>'开户行', 'ismust'=>0],
            ];
            return outputJson(2000, lang('code_2000'), $data);
        }
    }

    public function import_cert(Request $request)
    {
        if($request->isPost()){
            $param = $request->post();
            $optType = $request->param('type','add','string');
            $data = $param['data'];
            if(!is_array($data) || count($data) < 1){
                return outputJson(4000, '没有可导入的数据');
            }
            $error = '';
            foreach ($data as $k=>&$v){
                $hang = "行".($k + 1);

                if(!isset($v['person_name']) || empty($v['person_name'])){
                    $error = $hang."未输入人员姓名";break;
                }else{
                    $result = PersonModel::where(['deleted_user'=>0,'name'=>$v['person_name'],'comid'=>$this->userinfo['comid']])->find();
                    if(!$result){
                        $error = $hang."人员姓名不存在";break;
                    }

                    if($this->userinfo['token_roletype'] != 1){
                        $userChanid = (new UserService())->get_user_chanid();
                        if($userChanid != $result['chanid']){
                            $error = $hang."非本分公司人员";break;
                        }
                    }

                    $data[$k]['personid'] = $result['id'];
                    $data[$k]['chanid'] = $result['chanid'];
                    $data[$k]['deptid'] = $result['deptid'];
                    unset($data[$k]['person_name']);
                }

                if(!isset($v['type']) || empty($v['type'])){
                    $error = $hang."未输入证书分类";break;
                }else{
                    $data[$k]['type'] = (new ImportService())->get_basedata_keyid('cert_type',$v['type']);
                    if(!$data[$k]['type']){
                        $error = $hang."证书分类不存在";break;
                    }
                }

                if(!isset($v['name']) || empty($v['name'])){
                    $error = $hang."未输入证书名称";break;
                }
                if(!isset($v['number']) || empty($v['number'])){
                    $error = $hang."未输入证书编号";break;
                }else{
                    if((new CertService())->check_cert_number($v['number'])){
                        $error = $hang."证书编号已存在";break;
                    }
                }

                if(isset($v['major']) && !empty($v['major'])){
                    $data[$k]['major'] = str_replace('|','[]',$v['major']);
                }

                if(isset($v['expiry_dt']) && !empty($v['expiry_dt'])){
                    $data[$k]['expiry_dt'] = str_replace('|','[]',$v['expiry_dt']);
                }

                if(!isset($v['form']) || empty($v['form'])){
                    $error = $hang."未输入证书形式";break;
                }else{
                    if(!in_array($v['form'], (new CertService())->get_cert_form())){
                        $error = $hang."证书形式不存在";break;
                    }
                }

                if(!isset($v['state']) || empty($v['state'])){
                    $error = $hang."未输入证书状态";break;
                }else{
                    if(!in_array($v['state'], (new CertService())->get_cert_state())){
                        $error = $hang."证书状态不存在";break;
                    }
                }


                if(isset($v['issue_dt']) && !empty($v['issue_dt'])){
                    if(!check_date($v['issue_dt'], 1)){
                        $error = $hang."注册/发证日期格式错误";break;
                    }
                }

                if(isset($v['electron_dt']) && !empty($v['electron_dt'])){
                    if(!check_date($v['electron_dt'], 1)){
                        $error = $hang."电子件有效期格式错误";break;
                    }
                }

                if(isset($v['inspect_dt']) && !empty($v['inspect_dt'])){
                    if(!check_date($v['inspect_dt'], 1)){
                        $error = $hang."下次年检时间格式错误";break;
                    }
                }

                if(isset($v['store_addr']) && !empty($v['store_addr'])){
                    $data[$k]['store_addr'] = (new ImportService())->get_basedata_keyid('cert_store_addr',$v['store_addr']);
                    if(!$data[$k]['store_addr']){
                        $error = $hang."证书存放地不存在";break;
                    }
                }

                if(isset($v['custodianid']) && !empty($v['custodianid'])){
                    $exists_user = UserModel::where(['deleted_user'=>0,'realname'=>$v['custodianid'],'comid'=>$this->userinfo['comid']])->find();
                    if($exists_user){
                        $data[$k]['custodianid'] = $exists_user['id'];
                    }else{
                        $error = $hang."保管人不存在";break;
                    }
                }
            }
            if($error != ''){
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>$error]);
            }
            unset($v);
            Db::startTrans();
            try{
                foreach ($data as $k=>$v){
                    $v['comid'] = $this->userinfo['comid'];
                    $v['created_user'] = $this->userinfo['id'];
                    $v['created_at'] = date('Y-m-d H:i:s');
                    $certid = CertModel::insertGetId($v);

                    (new CertRelevan())->vate_cert_info($certid);

                    (new OptService())->add_flow_log(FlowLogModel::SET_IMPORT,'cert',$certid);
                    (new OptService())->add_log_list(FlowLogModel::SET_IMPORT, 'cert', $certid, '证件信息');
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                halt($e);
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>"操作失败"]);
            }
            return outputJson(2000, lang('code_2000'), ['status'=>1]);
        }else{
            $sheet = $request->param('sheet', '', 'int');
            $start = $request->param('start', 1, 'int');
            if($sheet === ''){
                return outputJson(4000, "请选择Sheet");
            }

            $data['data'] = (new Upload())->get_excel_sheet_data($sheet, $start);
            if(!$data['data'] || !is_array($data['data']) || count($data['data']) < 1){
                return outputJson(4000, "未获取到有效数据");
            }

            $data['field'] = [
                ['field'=>'person_name', 'name'=>'人员姓名', 'ismust'=>1],
                ['field'=>'type', 'name'=>'证书分类', 'ismust'=>1],
                ['field'=>'name', 'name'=>'证书名称', 'ismust'=>1],
                ['field'=>'number', 'name'=>'证书编号', 'ismust'=>1],
                ['field'=>'issue_unit', 'name'=>'发证单位', 'ismust'=>0],
                ['field'=>'issue_dt', 'name'=>'注册/发证日期', 'ismust'=>0],
                ['field'=>'major', 'name'=>'专业', 'ismust'=>0],
                ['field'=>'expiry_dt', 'name'=>'有效期止', 'ismust'=>0],
                ['field'=>'form', 'name'=>'证书形式', 'ismust'=>1],
                ['field'=>'state', 'name'=>'证书状态', 'ismust'=>1],
                ['field'=>'electron_dt', 'name'=>'电子件有效期', 'ismust'=>0],
                ['field'=>'inspect_dt', 'name'=>'下次年检时间', 'ismust'=>0],
                ['field'=>'custodianid', 'name'=>'保管人', 'ismust'=>0],
                ['field'=>'store_addr', 'name'=>'存放地', 'ismust'=>0],
                ['field'=>'explain', 'name'=>'备注', 'ismust'=>0],
            ];
            return outputJson(2000, lang('code_2000'), $data);
        }
    }

    public function import_cert_company(Request $request)
    {
        if($request->isPost()){
            $param = $request->post();
            $optType = $request->param('type','add','string');
            $data = $param['data'];
            if(!is_array($data) || count($data) < 1){
                return outputJson(4000, '没有可导入的数据');
            }
            $error = '';
            foreach ($data as $k=>$v){
                $hang = "行".($k + 1);

                if(!isset($v['chanid']) || empty($v['chanid'])){
                    $error = $hang."未输入所属单位";break;
                }else{
                    $result = DeptModel::where(['deleted_user'=>0,'name'=>$v['chanid']])->find();
                    if(!$result){
                        $error = $hang."所属单位不存在";break;
                    }
                    $data[$k]['chanid'] = $result['id'];
                }


                if(!isset($v['type']) || empty($v['type'])){
                    $error = $hang."未输入证书分类";break;
                }else{
                    $data[$k]['type'] = (new ImportService())->get_basedata_keyid('cert_company_type',$v['type']);
                    if(!$data[$k]['type']){
                        $error = $hang."证书分类不存在";break;
                    }
                }


                if(!isset($v['name']) || empty($v['name'])){
                    $error = $hang."未输入证书名称";break;
                }
                if(!isset($v['number']) || empty($v['number'])){
                    $error = $hang."未输入证书编号";break;
                }else{
                    if((new CertService())->check_cert_company_number($v['number'])){
                        $error = $hang."证书编号已存在";break;
                    }
                }

                if(!isset($v['form']) || empty($v['form'])){
                    $error = $hang."未输入证书形式";break;
                }else{
                    if(!in_array($v['form'], (new CertService())->get_cert_form())){
                        $error = $hang."证书形式不存在";break;
                    }
                }

                if(!isset($v['state']) || empty($v['state'])){
                    $error = $hang."未输入证书状态";break;
                }else{
                    if(!in_array($v['state'], (new CertService())->get_cert_state())){
                        $error = $hang."证书状态不存在";break;
                    }
                }

                if(isset($v['issue_dt']) && !empty($v['issue_dt'])){
                    if(!check_date($v['issue_dt'], 1)){
                        $error = $hang."注册/发证日期格式错误";break;
                    }
                }

                if(isset($v['expiry_dt']) && !empty($v['expiry_dt'])){
                    if(!check_date($v['expiry_dt'], 1)){
                        $error = $hang."有效期止格式错误";break;
                    }
                }

                if(isset($v['inspect_dt']) && !empty($v['inspect_dt'])){
                    if(!check_date($v['inspect_dt'], 1)){
                        $error = $hang."下次年检时间格式错误";break;
                    }
                }

                if(isset($v['store_addr']) && !empty($v['store_addr'])){
                    $data[$k]['store_addr'] = (new ImportService())->get_basedata_keyid('cert_store_addr',$v['store_addr']);
                    if(!$data[$k]['store_addr']){
                        $error = $hang."证书存放地不存在";break;
                    }
                }

                if(isset($v['custodianid']) && !empty($v['custodianid'])){
                    $exists_user = UserModel::where(['deleted_user'=>0,'realname'=>$v['custodianid'],'comid'=>$this->userinfo['comid']])->find();
                    if($exists_user){
                        $data[$k]['custodianid'] = $exists_user['id'];
                    }else{
                        $error = $hang."保管人不存在";break;
                    }
                }
            }
            if($error != ''){
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>$error]);
            }
            Db::startTrans();
            try{
                foreach ($data as $k=>$v){
                    $v['comid'] = $this->userinfo['comid'];
                    $v['created_user'] = $this->userinfo['id'];
                    $v['created_at'] = date('Y-m-d H:i:s');
                    $certid = CertCompanyModel::insertGetId($v);

                    (new CertRelevan())->vate_cert_company_info($certid);

                    (new OptService())->add_flow_log(FlowLogModel::SET_IMPORT,'cert_company',$certid);
                    (new OptService())->add_log_list(FlowLogModel::SET_IMPORT, 'cert_company', $certid, '证照信息');
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>$e->getMessage()]);
            }
            return outputJson(2000, lang('code_2000'), ['status'=>1]);
        }else{
            $sheet = $request->param('sheet', '', 'int');
            $start = $request->param('start', 1, 'int');
            if($sheet === ''){
                return outputJson(4000, "请选择Sheet");
            }

            $data['data'] = (new Upload())->get_excel_sheet_data($sheet, $start);
            if(!$data['data'] || !is_array($data['data']) || count($data['data']) < 1){
                return outputJson(4000, "未获取到有效数据");
            }

            $data['field'] = [
                ['field'=>'chanid', 'name'=>'所属单位', 'ismust'=>1],
                ['field'=>'type', 'name'=>'证书分类', 'ismust'=>1],
                ['field'=>'name', 'name'=>'证书名称', 'ismust'=>1],
                ['field'=>'number', 'name'=>'证书编号', 'ismust'=>1],
                ['field'=>'zinumber', 'name'=>'自编号', 'ismust'=>0],
                ['field'=>'introduce', 'name'=>'证书内容', 'ismust'=>0],
                ['field'=>'form', 'name'=>'证书形式', 'ismust'=>1],
                ['field'=>'state', 'name'=>'证书状态', 'ismust'=>1],
                ['field'=>'issue_unit', 'name'=>'发证单位', 'ismust'=>0],
                ['field'=>'issue_dt', 'name'=>'发证日期', 'ismust'=>0],
                ['field'=>'expiry_dt', 'name'=>'有效期止', 'ismust'=>0],
                ['field'=>'inspect_dt', 'name'=>'下次年检时间', 'ismust'=>0],
                ['field'=>'custodianid', 'name'=>'保管人', 'ismust'=>0],
                ['field'=>'store_addr', 'name'=>'存放地', 'ismust'=>0],
                ['field'=>'explain', 'name'=>'备注', 'ismust'=>0],
            ];
            return outputJson(2000, lang('code_2000'), $data);
        }
    }

    public function import_cert_empoly(Request $request)
    {
        if($request->isPost()){
            $param = $request->post();
            $optType = $request->param('type','add','string');
            $data = $param['data'];
            if(!is_array($data) || count($data) < 1){
                return outputJson(4000, '没有可导入的数据');
            }
            $error = '';
            foreach ($data as $k=>&$v){
                $hang = "行".($k + 1);

                if(!isset($v['personid']) || empty($v['personid'])){
                    $error = $hang."未输入持证人";break;
                }else{
                    $result = PersonModel::where(['deleted_user'=>0,'name'=>$v['personid']])->find();
                    if(!$result){
                        $error = $hang."持证人不存在";break;
                    }
                    $data[$k]['personid'] = $result['id'];
                }

                if(!isset($v['nature']) || empty($v['nature'])){
                    $error = $hang."未输入人员性质";break;
                }else{
                    if(!in_array($v['nature'], (new CertService())->get_person_nature())){
                        $error = $hang."人员性质不存在";break;
                    }
                }

                if(isset($v['channel']) && !empty($v['channel'])){
                    $is_base = (new ImportService())->check_basedata('cert_employ_channel',$v['channel']);
                    if($is_base !== true){
                        $error = $hang."聘用渠道错误，请输入以下".$is_base."其中一种";break;
                    }
                    $data[$k]['channel'] = (new ImportService())->get_basedata_keyid('cert_employ_channel',$v['channel']);
                    if(!$data[$k]['channel']){
                        $error = $hang."聘用渠道不存在";break;
                    }
                }


                if(!isset($v['name']) || empty($v['name'])){
                    $error = $hang."未输入合同名称";break;
                }


                if(isset($v['sign_dt']) && !empty($v['sign_dt'])){
                    if(!check_date($v['sign_dt'], 1)){
                        $error = $hang."签约日期格式错误";break;
                    }
                }

                if(!isset($v['qualification']) || empty($v['qualification'])){
                    $error = $hang."未输入聘用资格";break;
                }else{
                    $qualification = explode(',',$v['qualification']);
                    $qualificationArr = [];
                    foreach ($qualification as $vv){
                        $keyid = (new ImportService())->get_basedata_keyid('cert_employ_qualification',$vv);
                        if(!$keyid){
                            $error = $hang."聘用资格 - $vv - 不存在";break;
                        }else{
                            $qualificationArr[] = $keyid;
                        }
                    }
                    $data[$k]['qualification'] = implode(',',$qualificationArr);
                }

                if(isset($v['purpose']) && !empty($v['purpose'])){
                    $data[$k]['purpose'] = (new ImportService())->get_basedata_keyid('cert_employ_purpose',$v['purpose']);
                    if(!$data[$k]['purpose']){
                        $error = $hang."用途不存在";break;
                    }
                }

                if(isset($v['iszhu']) && !empty($v['iszhu'])){
                    if(!in_array($v['iszhu'], ['是', '否'])){
                        $error = $hang."是否可驻场填写错误";break;
                    }
                }

                if(!isset($v['money']) || $v['money'] == ''){
                    $error = $hang."未输入年聘费";break;
                }else{
                    if(!is_numeric($v['money'])){
                        $error = $hang."年聘费格式错误";break;
                    }
                }

                if(isset($v['subsidy']) && $v['subsidy'] == ''){
                    if(!is_numeric($v['subsidy'])){
                        $error = $hang."项目补贴格式错误";break;
                    }
                }

                if(isset($v['out_fee']) && $v['out_fee'] == ''){
                    if(!is_numeric($v['out_fee'])){
                        $error = $hang."出场费格式错误";break;
                    }
                }

                if(!isset($v['pay_type']) || empty($v['pay_type'])){
                    $error = $hang."未输入聘费支付方式";break;
                }else{
                    if(!in_array($v['pay_type'], (new CertService())->get_cert_pay_type())){
                        $error = $hang."聘费支付方式不存在";break;
                    }else{
                        if($v['pay_type'] == '月付'){
                            $data[$k]['pay_type'] = 1;
                        }
                        if($v['pay_type'] == '年付'){
                            $data[$k]['pay_type'] = 2;
                        }
                    }
                }

                if(!isset($v['start_dt']) || empty($v['start_dt'])){
                    $error = $hang."未输入聘费开始时间";break;
                }else{
                    if(!check_date($v['start_dt'], 1)){
                        $error = $hang."聘费开始时间格式错误";break;
                    }
                }

                if(!isset($v['end_dt']) || empty($v['end_dt'])){
                    $error = $hang."未输入聘费结束时间";break;
                }else{
                    if(!check_date($v['end_dt'], 1)){
                        $error = $hang."聘费结束时间格式错误";break;
                    }
                }
            }
            if($error != ''){
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>$error]);
            }
            unset($v);

            Db::startTrans();
            try{
                foreach ($data as $k=>$v){
                    $v['comid'] = $this->userinfo['comid'];
                    $v['created_user'] = $this->userinfo['id'];
                    $v['created_at'] = date('Y-m-d H:i:s');
                    $v['chanid'] = (new UserService())->get_user_chanid();
                    $v['deptid'] = $this->userinfo['deptid'];
                    $empid = CertEmployModel::insertGetId($v);

                    $minfo = CertEmployModel::where(['id'=>$empid])->find();
                    (new CertRelevan())->generate_cert_employ_money($minfo);

                    (new OptService())->add_flow_log(FlowLogModel::SET_IMPORT,'cert_employ',$empid);
                    (new OptService())->add_log_list(FlowLogModel::SET_IMPORT, 'cert_employ', $empid, '聘费合同');
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                halt($e->getMessage());
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>"操作失败"]);
            }
            return outputJson(2000, lang('code_2000'), ['status'=>1]);
        }else{
            $sheet = $request->param('sheet', '', 'int');
            $start = $request->param('start', 1, 'int');
            if($sheet === ''){
                return outputJson(4000, "请选择Sheet");
            }

            $data['data'] = (new Upload())->get_excel_sheet_data($sheet, $start);
            if(!$data['data'] || !is_array($data['data']) || count($data['data']) < 1){
                return outputJson(4000, "未获取到有效数据");
            }

            $data['field'] = [
                ['field'=>'personid', 'name'=>'持证人', 'ismust'=>1],
                ['field'=>'nature', 'name'=>'人员性质', 'ismust'=>1],
                ['field'=>'name', 'name'=>'合同名称', 'ismust'=>1],
                ['field'=>'sign_dt', 'name'=>'签约日期', 'ismust'=>0],
                ['field'=>'qualification', 'name'=>'聘用资格', 'ismust'=>1],
                ['field'=>'channel', 'name'=>'聘用渠道', 'ismust'=>0],
                ['field'=>'employ_expalin', 'name'=>'聘用说明', 'ismust'=>0],

                ['field'=>'purpose', 'name'=>'证件用途', 'ismust'=>0],
                ['field'=>'iszhu', 'name'=>'是否可驻场', 'ismust'=>0],
                ['field'=>'pay_type', 'name'=>'聘费支付方式', 'ismust'=>1],
                ['field'=>'money', 'name'=>'聘费(元)', 'ismust'=>1],
                ['field'=>'pin_explain', 'name'=>'聘费说明', 'ismust'=>0],

                ['field'=>'start_dt', 'name'=>'聘期开始时间', 'ismust'=>1],
                ['field'=>'end_dt', 'name'=>'聘期结束时间', 'ismust'=>1],
                ['field'=>'subsidy', 'name'=>'项目补贴(元/月)', 'ismust'=>0],
                ['field'=>'out_fee', 'name'=>'出场费(元/次)', 'ismust'=>0],
                ['field'=>'account', 'name'=>'银行账户名称', 'ismust'=>0],
                ['field'=>'account_num', 'name'=>'银行账号', 'ismust'=>0],
                ['field'=>'open_bank', 'name'=>'开户行', 'ismust'=>0],
                ['field'=>'explain', 'name'=>'备注', 'ismust'=>0],
            ];
            return outputJson(2000, lang('code_2000'), $data);
        }
    }

    public function import_user(Request $request)
    {
        if($request->isPost()){
            $param = $request->post();
            $optType = $request->param('type','add','string');
            $data = $param['data'];
            if(!is_array($data) || count($data) < 1){
                return outputJson(4000, '没有可导入的数据');
            }

            $error = '';
            foreach ($data as $k=>&$v){
                $hang = "行".($k + 1);

                if(!isset($v['realname']) || empty($v['realname'])){
                    $error = $hang."未输入姓名";break;
                }else{
                    if((new UserService())->check_user_name($v['realname'])){
                        $error = $hang."用户姓名已存在";break;
                    }
                }

                if(!isset($v['tel']) || empty($v['tel'])){
                    $error = $hang."未输入登录手机号";break;
                }else{
                    if(!check_phone($v['tel'])){
                        $error = $hang."手机号码格式错误";break;
                    }
                    if((new UserService())->check_login_tel($v['tel'])){
                        $error = $hang."登录手机号码已存在";break;
                    }
                }

                if(!isset($v['pass']) || empty($v['pass'])){
                    $v['pass'] = $v['tel'];
                }


                if(!isset($v['deptid']) || empty($v['deptid'])){
                    $error = $hang."未输入所属部门";break;
                }else{
                    $result = DeptModel::where(['deleted_user'=>0,'name'=>$v['deptid']])->find();
                    if(!$result){
                        $error = $hang."所属部门不存在";break;
                    }
                    $data[$k]['deptid'] = $result['id'];
                }


                if(isset($v['gangid']) && !empty($v['gangid'])){
                    $gangArr = explode(',', $v['gangid']);
                    $gangidArr = [];
                    foreach ($gangArr as $gangname){
                        $gid = UserGangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$gangname])->value('id');
                        if(!$gid){
                            $error = $hang."岗位名称“".$gangname."”不存在";break;
                        }
                        $gangidArr[] = $gid;
                    }
                    $data[$k]['gangid'] = implode(',', $gangidArr);
                }else{
                    $data[$k]['gangid'] = '';
                }


                $userRoleType = 0;
                if(isset($v['roletype']) && !empty($v['roletype'])){
                    $role_type = (new UserService())->get_user_role_type();
                    foreach ($role_type as $rrr=>$ttt){
                        if($ttt == $v['roletype']){
                            $userRoleType = $rrr;
                        }
                    }
                    if($userRoleType<1){
                        $error = $hang."用户角色类型不存在";break;
                    }
                }
                unset($data[$k]['roletype']);

                if(isset($v['roleid']) && !empty($v['roleid']) && $userRoleType > 0){
                    $roleidArr = [];
                    $roleArr = explode(',', $v['roleid']);
                    foreach ($roleArr as $rolename){
                        $rid = UserRoleModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$rolename,'type'=>$userRoleType])->value('id');
                        if(!$rid){
                            $error = $hang."用户角色名称“".$rolename."”不存在";break;
                        }
                        $roleidArr[] = $rid;
                    }
                    $data[$k]['roleid'] = implode(',', $roleidArr);
                }else{
                    $data[$k]['roleid'] = '';
                }

                if(isset($v['status']) && !empty($v['status'])){
                    if(!in_array($v['status'], ['正常','停用'])){
                        $error = $hang."账号状态输入错误";break;
                    }
                    if($v['status'] == '正常'){
                        $data[$k]['status'] = 1;
                    }
                    if($v['status'] == '停用'){
                        $data[$k]['status'] = 0;
                    }
                }else{
                    $data[$k]['status'] = 0;
                }
            }

            if($error != ''){
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>$error]);
            }
            unset($v);
            Db::startTrans();
            try{
                foreach ($data as $k=>$v){
                    $v['comid'] = $this->userinfo['comid'];
                    $v['created_user'] = $this->userinfo['id'];
                    $v['created_at'] = date('Y-m-d H:i:s');
                    $v['pass'] = md5(md5($v['pass']));
                    $userid = UserModel::insertGetId($v);
                    (new CertRelevan())->vate_person_info($userid);
                    (new OptService())->add_flow_log(FlowLogModel::SET_IMPORT,'user',$userid);
                    (new OptService())->add_log_list(FlowLogModel::SET_IMPORT, 'user', $userid, '用户信息');
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                halt($e->getMessage());
                return outputJson(2000, lang('code_2000'), ['status'=>0, 'msg'=>"操作失败"]);
            }
            return outputJson(2000, lang('code_2000'), ['status'=>1]);
        }else{
            $sheet = $request->param('sheet', '', 'int');
            $start = $request->param('start', 1, 'int');
            if($sheet === ''){
                return outputJson(4000, "请选择Sheet");
            }

            $data['data'] = (new Upload())->get_excel_sheet_data($sheet, $start);
            if(!$data['data'] || !is_array($data['data']) || count($data['data']) < 1){
                return outputJson(4000, "未获取到有效数据");
            }

            $data['field'] = [
                ['field'=>'realname', 'name'=>'姓名', 'ismust'=>1],
                ['field'=>'tel', 'name'=>'登录手机号', 'ismust'=>1],
                ['field'=>'pass', 'name'=>'登录密码', 'ismust'=>1],
                ['field'=>'deptid', 'name'=>'所属部门', 'ismust'=>1],
                ['field'=>'gangid', 'name'=>'岗位', 'ismust'=>0],
                ['field'=>'roletype', 'name'=>'用户角色类型', 'ismust'=>0],
                ['field'=>'roleid', 'name'=>'用户角色名称', 'ismust'=>0],
                ['field'=>'status', 'name'=>'账号状态', 'ismust'=>0],
                ['field'=>'explain', 'name'=>'备注', 'ismust'=>0],
            ];
            return outputJson(2000, lang('code_2000'), $data);
        }
    }





    /**
     * 通用导入 只返回 excel文件内容【适用范围：如新增编辑页面明细导入】
     * @param Request $request
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function import_data(Request $request)
    {
        $sheet = $request->param('sheet', '', 'int');
        $start = $request->param('start', 1, 'int');
        if($sheet === ''){
            return outputJson(4000, "请选择Sheet");
        }

        $data['data'] = (new Upload())->get_excel_sheet_data($sheet, $start);
        if(!$data['data'] || !is_array($data['data']) || count($data['data']) < 1){
            return outputJson(4000, "未获取到有效数据");
        }
        return outputJson(2000, lang('code_2000'), $data);
    }

}

















